//////////////////////////////////////////////////////////////////////////////////
//	Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include <wx/hyperlink.h>
#include <wx/html/htmlwin.h>
#include <assert.h>
#include "conectwizard.h"
#include "rtelnetsocket.h"
#include "telnetlink.h"
#include "sysdepapi.h"
#include "rcmdapp.h"
#include "telnetwindow.h"
#include "progressdlg.h"

enum cIDS
{
	cIdProtoSelCombo=14768,
	cIdSSHAdvancedCheckBox,
	cIDBrowsePubKeyButton,
	cIDBrowsePrivKeyButton,
	cIdAdvancedCheckBox

};

BEGIN_EVENT_TABLE(CConectWizard, wxWizard)
	EVT_BUTTON(wxID_BACKWARD, CConectWizard::OnBack)
END_EVENT_TABLE()

CConectWizard::CConectWizard(wxFrame* pFrame):
	wxWizard(pFrame,wxID_ANY, _("Router connection Wizard"),
    wxBitmap(wxGetApp().GetImageDir()+wxT("wizard.png"), wxBITMAP_TYPE_PNG),wxDefaultPosition)
{
	
}

CConectWizard::~CConectWizard()
{
	
}

void CConectWizard::GetConParams(CONPARAMS& params)
{
	m_params = params;
	 
	m_pFirstPage	= new CRouterAddressInputPage(this);
	m_pTelnetPage	= new CConParamInputPage(this);
	m_pSSHPage		= new CSSHParamInputPage(this);
	
	FitToPage(m_pFirstPage);
	
	if ( !m_pFirstPage || !m_pTelnetPage || !m_pSSHPage ) THROW_EXC(cERNoFreeMem);

    wxWizardPageSimple::Chain(m_pFirstPage, m_pTelnetPage );
    
	if ( RunWizard( m_pFirstPage ) )
	{
		params = m_params;
	}
	else
		THROW_EXC(cEROperationCanceledByUser);
}

void CConectWizard::OnBack(wxCommandEvent& event)
{
	ShowPage( GetCurrentPage()->GetPrev(), false);
}

bool CConectWizard::CheckHost()
{
	bool bRes(false);

	CRTelnetSocket socket;

	::wxBeginBusyCursor();

	//CProgressDlg* pProgress = CProgressDlg::CreateProgressDlg(this, _("Operation in progress, connecting to the router"), CONNECT_TO_ROUTER_TIME_OUT/100);

	try
	{
		//Must run in new thread 
		char buf[128];
		socket.Connect(m_params.strIP.ToAscii(), m_params.wPort);
		socket.Read(buf, sizeof(buf));

		bRes = true;
	}
	catch(...)
	{
	}

	//delete pProgress;

	::wxEndBusyCursor();
	
	return bRes;
}

bool CConectWizard::CheckAll(wxString& strErr, wxString& strSession, int& errorID)
{
	bool bRes(false);

	CTelnetLink link;

	errorID = 0;

	::wxBeginBusyCursor();

	try
	{
		link.Connect( m_params );

		bRes = true;
	}
	catch(CRTException& exc)
	{
		strSession = wxString::FromAscii( link.GetLog().c_str());

		errorID = exc.GetReason();

		switch( errorID )
		{
			case cERWrongUserOrPassword:
			{
				strErr = _("Wrong username or password.");
			}
			break;

			case cERFailed:
			{
				strErr = _("Your router is not compatible. Please, send router model to developer for investigation.");
			}
			break;

			case cERInvalidHostOrPort:
			{
				strErr = _("Unable to connect to host!\nCheck host address or port and try again.");
			}
			break;

			case cERStartSSHConversation:
			{
				strErr = _("Unable to start SSH session!\nCheck host address or port and try again.");
			}
			break;

			case cERSSHKeysEmpty:
			{
				strErr = _("SSH keys not defined.Use connection wizard to setup path to key files.");
			}
			break;

			case cERSSHKeysNotSupported:
			{
				strErr = _("SSH keys authentication not supported by remote host.");
			}
			break;

			case cERSSHKeysAuthFailed:
			{
				strErr = _("SSH keys authentication failed.");
			}
			break;

			default:
			{
				strErr = wxString::Format( wxT("System error: %d"), exc.GetReason() );
			}
			break;
		}
	}

	::wxEndBusyCursor();
	
	return bRes;
}

/////////////////////////////////////////////////////////////////////////////////////////
//CConParamInputPage
BEGIN_EVENT_TABLE(CConParamInputPage, wxWizardPageSimple)
	EVT_CHECKBOX(cIdAdvancedCheckBox, CConParamInputPage::OnTogleAdvanced)
END_EVENT_TABLE()

CConParamInputPage::CConParamInputPage(CConectWizard *parent):
	wxWizardPageSimple(parent), m_parent(parent)
{
	CONPARAMS params = parent->GetConParams();

	wxBoxSizer *hbox = new wxBoxSizer(wxVERTICAL);
	m_fgs = new wxFlexGridSizer(5, 2, 10, 5);

	wxBoxSizer *boxTips = new wxBoxSizer(wxVERTICAL);
	wxHtmlWindow* pHTMLWindow = new wxHtmlWindow(this,-1/*, wxDefaultPosition, wxSize(200,100), wxHW_SCROLLBAR_AUTO*/ );
	wxString str(wxT("<html>"));
	wxColour color = GetBackgroundColour(); 
	str+=wxString::Format(wxT("<body bgcolor=\"%s\">"), color.GetAsString(wxC2S_HTML_SYNTAX).c_str());
	str+=wxT("<font size='2'><center><b>");
	str+=_("User Name and Password Tips");
	str+=wxT("</b></center>");
	str+=wxT("<p>");
	str+=_("User name should be an Admin/admin/root or other according to the router guide. ");
	str+=_("Try admin as user name and your admin web interface password first. ");
	str+=wxT("</font></body></html>");
	pHTMLWindow->SetPage( str );
	boxTips->Add(pHTMLWindow,1, wxALL|wxCENTER|wxEXPAND, 5);
	hbox->Add(boxTips, 1, wxALL|wxCENTER|wxEXPAND, 5);

	m_pcbHTTP = new wxCheckBox(this, -1, _("Netgear telnet enabler:"));
	m_pedtHTTP = new wxTextCtrl(this, -1, params.strHTTP, wxDefaultPosition, wxDefaultSize);

	m_pcbHTTP->SetValue( params.bHTTPEnable );

	m_plbWelcome= new wxStaticText(this, -1, _("Login prompt:"));
	m_plbShell	= new wxStaticText(this, -1, _("Shell command:"));

	m_pedtWelcome= new wxTextCtrl(this, -1, params.strLoginPrompt, wxDefaultPosition, wxDefaultSize );
	m_pedtShell = new wxTextCtrl(this, -1, params.strShell, wxDefaultPosition, wxDefaultSize);

	wxStaticText *ptlUsr  = new wxStaticText(this, -1, _("User:"));
	wxStaticText *ptlPassw= new wxStaticText(this, -1, _("Password:"));
	
	m_ptcUser 	= new wxTextCtrl(this, -1, params.strUsr, wxDefaultPosition, wxDefaultSize );
	m_ptcPassw 	= new wxTextCtrl(this, -1, params.strPassw, wxDefaultPosition, wxDefaultSize, wxTE_PASSWORD);

	m_fgs->Add(m_pcbHTTP);
	m_fgs->Add(m_pedtHTTP, 1, wxEXPAND);

	m_fgs->Add(m_plbWelcome);
	m_fgs->Add(m_pedtWelcome, 1, wxEXPAND);

	m_fgs->Add(ptlUsr);
	m_fgs->Add(m_ptcUser, 1, wxEXPAND);
		
	m_fgs->Add(ptlPassw);
	m_fgs->Add(m_ptcPassw, 1, wxEXPAND);

	m_fgs->Add(m_plbShell);
	m_fgs->Add(m_pedtShell, 1, wxEXPAND);
	
	m_fgs->AddGrowableCol(1, 1);

	m_pAdvanced = new wxCheckBox(this, cIdAdvancedCheckBox, _("Advanced settings") );
	
	hbox->Add(m_fgs, 1, wxALIGN_CENTER, 10);
	hbox->Add(m_pAdvanced);

    SetSizer(hbox);
    //hbox->Fit(this);

	SetPrev( parent->GetFirstPage() );

	wxCommandEvent event;
	OnTogleAdvanced(event);
}

void CConParamInputPage::OnTogleAdvanced(wxCommandEvent& event)
{
	bool bShow = m_pAdvanced->GetValue();

	
	m_fgs->Show( m_pedtHTTP, bShow );
	m_fgs->Show( m_pcbHTTP, bShow );

	m_fgs->Show( m_plbWelcome, bShow );
	m_fgs->Show( m_plbShell, bShow );

	m_fgs->Show(m_pedtWelcome, bShow );
	m_fgs->Show(m_pedtShell, bShow );

	GetSizer()->CalcMin();
	GetSizer()->RecalcSizes();

	Refresh();
}

bool CConParamInputPage::TransferDataFromWindow()
{
	bool bRes(true);
	
	assert(m_parent);
	
	CONPARAMS	params = m_parent->GetConParams();
	
	params.strUsr= m_ptcUser->GetValue();
	params.strPassw=m_ptcPassw->GetValue();

	params.strLoginPrompt= m_pedtWelcome->GetValue();
	params.strShell=m_pedtShell->GetValue();

	params.bHTTPEnable = m_pcbHTTP->GetValue();
	params.strHTTP = m_pedtHTTP->GetValue();

	if ( params.bHTTPEnable && params.strHTTP.empty() )
	{
		m_pedtHTTP->SetFocus();
		bRes = false;
	}
	
	//User
	/*
	if ( bRes && params.strUsr.IsEmpty() )
	{
		m_ptcUser->SetFocus();
		bRes = false;
	}
	
	//Password
	if ( bRes && params.strPassw.IsEmpty() )
	{
		m_ptcPassw->SetFocus();
		bRes = false;
	}*/

	if ( !bRes )
    {
		wxMessageBox(_("Empty field!"), _("Error"),
                         wxICON_WARNING | wxOK, this);
    }
	else
	{
		wxString str, strLog;
		m_parent->SetConParams(params);

		int nErrorID(0);

		if ( !m_parent->CheckAll(str, strLog, nErrorID) )
		{
			bRes = false;

			CTelnetWindow wnd(false, nErrorID, str, strLog);
			wnd.ShowModal();

			/*
			wxMessageBox(str, _("Error"),
                        wxICON_WARNING | wxOK, this);
			*/
		}
	}

    return bRes;
}

/////////////////////////////////////////////////////////////////////////////////////////
//CRouterAddressInputPage
BEGIN_EVENT_TABLE(CRouterAddressInputPage, wxWizardPageSimple)
	EVT_TEXT(cIdProtoSelCombo, 	CRouterAddressInputPage::OnProtoChanged)
END_EVENT_TABLE()

CRouterAddressInputPage::CRouterAddressInputPage(CConectWizard *parent):
	wxWizardPageSimple(parent), m_parent(parent)
{
	CONPARAMS params = parent->GetConParams();
	wxBoxSizer *hbox = new wxBoxSizer(wxVERTICAL);

	
	wxBoxSizer *boxTips = new wxBoxSizer(wxVERTICAL);
	wxHtmlWindow* pHTMLWindow = new wxHtmlWindow(this,-1/*, wxDefaultPosition, wxSize(200,100), wxHW_SCROLLBAR_AUTO*/ );
	wxString str(wxT("<html>"));
	wxColour color = GetBackgroundColour(); 
	str+=wxString::Format(wxT("<body bgcolor=\"%s\">"), color.GetAsString(wxC2S_HTML_SYNTAX).c_str());
	str+=wxT("<font size='2'><center><b>");
	str+=_("Connection Tips");
	str+=wxT("</b></center>");
	str+=wxT("<p>");
	str+=_("Make sure telnet/ssh access is enabled in the router. To enable telnet access, login to the router using web interface and enable telnet/ssh according to the user guide. ");
	str+=wxT("</font></body></html>");
	pHTMLWindow->SetPage( str );
	boxTips->Add(pHTMLWindow,1, wxALL|wxCENTER|wxEXPAND, 5);
	hbox->Add(boxTips, 1, wxALL|wxCENTER|wxEXPAND, 5);
	

	wxFlexGridSizer *fgs = new wxFlexGridSizer(3, 2, 10, 5);
	
	wxStaticText *ptProto = new wxStaticText(this, -1, _("Protocol:"));
	wxStaticText *ptlIP   = new wxStaticText(this, -1, _("Router IP:"));
	wxStaticText *ptlPort = new wxStaticText(this, -1, _("Port:"));
	
	m_strPort = wxT("23");
	
	wxString strIp = wxString::FromAscii( CSysDepAPI::GetDefGateWay().c_str() );

	int nId;
	m_pcProtocol = new wxComboBox(this, cIdProtoSelCombo, wxT(""), wxDefaultPosition, wxDefaultSize, 0, NULL, wxCB_DROPDOWN|wxCB_READONLY);
	nId = m_pcProtocol->Append ( _("Telnet"), (void*)cPROTOCOLTelnet);

	wxString strDefPort;

	if ( params.cProto ==cPROTOCOLTelnet )
	{
		m_pcProtocol->SetSelection(nId);
		strDefPort = wxT("23");
	}

	nId = m_pcProtocol->Append ( _("SSH"),    (void*)cPROTOCOLSSH);

	if ( params.cProto ==cPROTOCOLSSH )
	{
		m_pcProtocol->SetSelection(nId);
		strDefPort = wxT("22");
	}

	if ( params.wPort )
	{
		m_strPort = strDefPort = wxString::Format( wxT("%u"), params.wPort );
	}

	if ( !params.strIP.IsEmpty() )
	{
		strIp = params.strIP;
	}


	m_ptcIP		= new wxTextCtrl(this, -1, strIp );
	m_ptcPort	= new wxTextCtrl(this, -1, strDefPort,  wxDefaultPosition, wxDefaultSize, 0,
		wxTextValidator(wxFILTER_NUMERIC, &m_strPort));


	fgs->Add(ptProto);
	fgs->Add(m_pcProtocol, 1, wxEXPAND);
	
	fgs->Add(ptlIP);
	fgs->Add(m_ptcIP, 1, wxEXPAND);

	fgs->Add(ptlPort);
	fgs->Add(m_ptcPort, 1, wxRIGHT);
			
	fgs->AddGrowableCol(1, 1);
	
	hbox->Add(fgs, 1, wxALIGN_CENTER, 10);
		
    SetSizer(hbox);
    hbox->Fit(this);
}

bool CRouterAddressInputPage::TransferDataFromWindow()
{
	bool bRes(true);
	
	assert(m_parent);
	
	CONPARAMS	params = m_parent->GetConParams();


	//Protocol
	void* pData = m_pcProtocol->GetClientData( m_pcProtocol->GetCurrentSelection());
	params.cProto = (cPROTOCOL)(long)pData;

	
	params.strIP = m_ptcIP->GetValue();
	params.strIP = params.strIP.Trim();
	params.strIP = params.strIP.Trim(false);
	
	//IP
	if ( params.strIP.IsEmpty() )
	{
		m_ptcIP->SetFocus();
		bRes = false;
	}
	
	//Port
	if ( bRes && m_ptcPort->GetValue().IsEmpty() )
	{
		m_ptcPort->SetFocus();
		bRes = false;
	}
	
	//check port range
	if ( bRes  )
	{
		long val=0;
		
		if ( !m_ptcPort->GetValue().ToLong(&val) ||
			 val>0xFFFF ||
			 val<1 )
		{
			wxMessageBox(_("Port value out of range!"), _("Error"),
	                         wxICON_WARNING | wxOK, this);
			
			return false;
		}

		params.wPort = (unsigned short)val;
		
	}
	
	if ( !bRes )
    {
		wxMessageBox(_("Empty field!"), _("Error"),
                         wxICON_WARNING | wxOK, this);
    }
	else
	{
		m_parent->SetConParams(params);

		if ( !m_parent->CheckHost() )
		{
			bRes = false;

			wxMessageBox(_("Unable to connect to the host! Check host address or port and try again. Your router may not have telnet/SSH access support or telnet/SSH was disabled. Check router's user guide to enable telnet access. Without telnet or SSH access, Router Commander works as Syslog server only and free of charge."), _("Error"),
						wxICON_WARNING | wxOK, this);
		}

		if ( cPROTOCOLSSH == params.cProto )
		{
			SetNext( m_parent->GetSSHWizard() );
		}
		else
		{
			SetNext( m_parent->GetTelnetWizard() );
		}
	}

    return bRes;
}

/////////////////////////////////////////////////////////////////////////////////////////
//Events
void CRouterAddressInputPage::OnProtoChanged(wxCommandEvent&)
{
	void* pData = m_pcProtocol->GetClientData( m_pcProtocol->GetCurrentSelection());
	cPROTOCOL proto = (cPROTOCOL)(long)pData;

	wxString strDefPort;

	if ( proto==cPROTOCOLTelnet )
	{
		strDefPort = wxT("23");
	}
	else
	{
		strDefPort = wxT("22");
	}

	m_ptcPort->SetValue(strDefPort);

}

/////////////////////////////////////////////////////////////////////////////////////////
//CConParamFinishPage
CConParamFinishPage::CConParamFinishPage(CConectWizard *parent):
	wxWizardPageSimple(parent), m_bConOk(false), m_parent(parent)
{
	
}

/////////////////////////////////////////////////////////////////////////////////////////
//CSSHParamInputPage
BEGIN_EVENT_TABLE(CSSHParamInputPage, wxWizardPageSimple)
	EVT_BUTTON(cIDBrowsePubKeyButton, CSSHParamInputPage::OnPubKeyFile)
	EVT_BUTTON(cIDBrowsePrivKeyButton, CSSHParamInputPage::OnPrivKeyFile)
	EVT_CHECKBOX(cIdSSHAdvancedCheckBox, CSSHParamInputPage::OnTogleAdvanced)
END_EVENT_TABLE()

CSSHParamInputPage::CSSHParamInputPage(CConectWizard *parent):
	wxWizardPageSimple(parent), m_parent(parent)
{
	m_params = parent->GetConParams();

	m_hbox = new wxBoxSizer(wxVERTICAL);

	wxBoxSizer *boxTips = new wxBoxSizer(wxVERTICAL);
	wxHtmlWindow* pHTMLWindow = new wxHtmlWindow(this,-1/*, wxDefaultPosition, wxSize(200,100), wxHW_SCROLLBAR_AUTO*/ );
	wxString str(wxT("<html>"));
	wxColour color = GetBackgroundColour(); 
	str+=wxString::Format(wxT("<body bgcolor=\"%s\">"), color.GetAsString(wxC2S_HTML_SYNTAX).c_str());
	str+=wxT("<font size='2'><center><b>");
	str+=_("User Name and Password Tips");
	str+=wxT("</b></center>");
	str+=wxT("<p>");
	str+=_("User name should be an Admin/admin/root or other according to the router guide. ");
	str+=_("Try admin as user name and your admin web interface password first. ");
	str+=wxT("</font></body></html>");
	pHTMLWindow->SetPage( str );
	boxTips->Add(pHTMLWindow,1, wxALL|wxCENTER|wxEXPAND, 5);
	m_hbox->Add(boxTips, 1, wxALL|wxCENTER|wxEXPAND, 5);

	m_fgsUserPass = new wxFlexGridSizer(3, 2, 9, 25);

	wxStaticText *ptlUsr  = new wxStaticText(this, -1, _("User:"));
	wxStaticText *ptlPassw= new wxStaticText(this, -1, _("Password:"));
	
	m_ptcUser 	= new wxTextCtrl(this, -1, m_params.strUsr, wxDefaultPosition, wxDefaultSize );
	m_ptcPassw 	= new wxTextCtrl(this, -1, m_params.strPassw, wxDefaultPosition, wxDefaultSize, wxTE_PASSWORD);

	m_fgsUserPass->Add(ptlUsr);
	m_fgsUserPass->Add(m_ptcUser, 1, wxEXPAND);
		
	m_fgsUserPass->Add(ptlPassw);
	m_fgsUserPass->Add(m_ptcPassw, 1, wxEXPAND);
	
	m_fgsUserPass->AddGrowableCol(1, 1);
	m_hbox->Add(m_fgsUserPass, 1, wxALIGN_CENTER, 20);

	
	///////////////////////////////////////////////////////////////////
	m_pAdvanced = new wxCheckBox(this, cIdSSHAdvancedCheckBox, _("Use SSH Keys") );
	m_pAdvanced->SetValue( m_params.bEnableSSHKey );

	m_fgsPKI = new wxFlexGridSizer(4, 2, 10, 5);

	wxStaticText *ptlUsr2  = new wxStaticText(this, -1, _("User:"));
	m_ptcSSHUser 	= new wxTextCtrl(this, -1, m_params.strUsr, wxDefaultPosition, wxDefaultSize );

	m_fgsPKI->Add(ptlUsr2);
	m_fgsPKI->Add(m_ptcSSHUser, 1, wxEXPAND);

	wxBoxSizer *hAdvancedBoxPubKey = new wxBoxSizer(wxHORIZONTAL);
	m_plbPubKey		= new wxStaticText(this, -1, _("Public key path:"));
	m_pedtPubKey	= new wxTextCtrl(this, -1, m_params.strPublickey, wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
	m_pBtnPubKey	= new wxButton(this, cIDBrowsePubKeyButton, _T("..."), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT|wxNO_BORDER );
	hAdvancedBoxPubKey->Add(m_pedtPubKey);
	hAdvancedBoxPubKey->Add(m_pBtnPubKey);
	m_fgsPKI->Add(m_plbPubKey);
	m_fgsPKI->Add(hAdvancedBoxPubKey, 1, wxEXPAND);

	wxBoxSizer *hAdvancedBoxPrivKey = new wxBoxSizer(wxHORIZONTAL);

	m_plbPrivKey	= new wxStaticText(this, -1, _("Private key path:"));
	m_pedtPrivKey 	= new wxTextCtrl(this, -1, m_params.strPrivkey, wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
	m_pBtnPrivKey	= new wxButton(this, cIDBrowsePrivKeyButton, _T("..."), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT|wxNO_BORDER );
	hAdvancedBoxPrivKey->Add(m_pedtPrivKey);
	hAdvancedBoxPrivKey->Add(m_pBtnPrivKey);
	m_fgsPKI->Add(m_plbPrivKey);
	m_fgsPKI->Add(hAdvancedBoxPrivKey, 1, wxEXPAND);

	m_plbPassPhraze = new wxStaticText(this, -1, _("Passphrase:"));
	m_pedtPassPhraze= new wxTextCtrl(this, -1, m_params.strPassphrase, wxDefaultPosition, wxDefaultSize, wxTE_PASSWORD );
	m_fgsPKI->Add(m_plbPassPhraze);
	m_fgsPKI->Add(m_pedtPassPhraze, 1, wxEXPAND);

	m_fgsPKI->AddGrowableCol(1, 1);
	m_hbox->Add(m_fgsPKI, 1, wxALIGN_CENTER, 10);

	////////////////////////////////////////////////////////////////////
	m_hbox->Add(m_pAdvanced);

    SetSizer(m_hbox);
    m_hbox->Fit(this);

	wxCommandEvent event;
	OnTogleAdvanced(event);

	SetPrev( parent->GetFirstPage() );
}

bool CSSHParamInputPage::TransferDataFromWindow()
{
	bool bRes(true);
	
	assert(m_parent);
	
	CONPARAMS	params = m_parent->GetConParams();
	
	params.strUsr= m_ptcUser->GetValue();
	params.strPassw=m_ptcPassw->GetValue();

	params.bEnableSSHKey = m_pAdvanced->GetValue();
	params.strPublickey = m_pedtPubKey->GetValue();
	params.strPrivkey = m_pedtPrivKey->GetValue();
	params.strPassphrase = m_pedtPassPhraze->GetValue();

	if ( params.bEnableSSHKey )
	{
		params.strUsr= m_ptcSSHUser->GetValue();

		if ( params.strPublickey.IsEmpty() )
		{
			m_pedtPubKey->SetFocus();
			bRes = false;
		}

		if ( params.strPrivkey.IsEmpty() )
		{
			m_pedtPrivKey->SetFocus();
			bRes = false;
		}
	}
	
	//User
	/*
	if ( bRes && params.strUsr.IsEmpty() )
	{
		m_ptcUser->SetFocus();
		bRes = false;
	}
	
	//Password
	if ( bRes && params.strPassw.IsEmpty() )
	{
		m_ptcPassw->SetFocus();
		bRes = false;
	}*/

	if ( !bRes )
    {
		wxMessageBox(_("Empty field!"), _("Error"),
                         wxICON_WARNING | wxOK, this);
    }
	else
	{
		wxString str, strLog;
		m_parent->SetConParams(params);

		int nErrorID(0);

		if ( !m_parent->CheckAll(str, strLog, nErrorID) )
		{
			bRes = false;

			CTelnetWindow wnd(true, nErrorID, str, strLog);
			wnd.ShowModal();


			/*
			wxMessageBox(str, _("Error"),
                        wxICON_WARNING | wxOK, this);
			*/
		}
	}

    return bRes;
}

void CSSHParamInputPage::OnTogleAdvanced(wxCommandEvent& event)
{
	m_params.bEnableSSHKey = m_pAdvanced->GetValue();

	if ( m_params.bEnableSSHKey )
	{
		m_hbox->Show(m_fgsPKI, true);
		m_hbox->Show(m_fgsUserPass, false);
	}
	else
	{
		m_hbox->Show(m_fgsPKI, false);
		m_hbox->Show(m_fgsUserPass, true);
	}

	m_hbox->CalcMin();
	m_hbox->RecalcSizes();

	Refresh();
}

void CSSHParamInputPage::OnPubKeyFile(wxCommandEvent& event)
{
	try
	{
		wxFileDialog dlg(this, _("Choose a file"),
				wxT(""), wxT(""), wxT("*.*"), wxFD_OPEN|wxFD_FILE_MUST_EXIST );

		if ( wxID_OK ==dlg.ShowModal() )
		{
			m_pedtPubKey->SetValue(dlg.GetPath());
		}
	}
	catch(...)
	{
		wxMessageBox(_("Failed!"), _("Error"), wxICON_WARNING | wxOK, this);
	}
}

void CSSHParamInputPage::OnPrivKeyFile(wxCommandEvent& event)
{
	try
	{
		wxFileDialog dlg(this, _("Choose a file"),
				wxT(""), wxT(""), wxT("*.*"), wxFD_OPEN|wxFD_FILE_MUST_EXIST );

		if ( wxID_OK ==dlg.ShowModal() )
		{
			m_pedtPrivKey->SetValue(dlg.GetPath());
		}
	}
	catch(...)
	{
		wxMessageBox(_("Failed!"), _("Error"), wxICON_WARNING | wxOK, this);
	}
}
